
import isg.etsii.ull.es.DAO.PropiedadesDAO;
import isg.etsii.ull.es.Propiedad;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ServletBusqueda
 */
public class ServletBusqueda extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PropiedadesDAO PDAO = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletBusqueda() {
        super();
        
        try {
			PDAO = new PropiedadesDAO();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
   			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
   	   		dispatcher.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
   			try {
   				Busqueda(request, response);
   			} catch (ClassNotFoundException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			} catch (SQLException e) {
   				// TODO Auto-generated catch block
   				e.printStackTrace();
   			}	
	}
	
	protected void Busqueda(HttpServletRequest req, HttpServletResponse res) throws IOException, ClassNotFoundException, SQLException, ServletException {
		String rango = req.getParameter("rango");
		String dir = req.getParameter("dir");
		String tipo = req.getParameter("tipo");
		ArrayList<Propiedad> props = new ArrayList<Propiedad>();
		HttpSession session = req.getSession();
		
		int op = 0;
		float fin = 0;
		float ini = 0;
		
		if (rango.equals("null") && dir.equals("null") && tipo.equals("null")) {
			props = PDAO.all();
		} 
		else {
           if (!rango.equals("null")) {     	   
        	   switch (op = Integer.parseInt(req.getParameter("rango"))) {
	        	   case 1: {ini = 300000;
	                       break;}
	        	   
	        	   case 2: {ini = 300000;
	        	            fin = 600000;
                            break;}
	        	   
	        	   case 3: {ini = 600000;
                            break;}
        	   } // switch
        	   
               if (!dir.equals("null") && !tipo.equals("null")) {
            	   op += 9;
               }
               else if (!tipo.equals("null")) {
            	   op += 6;
               }
               else if (!dir.equals("null")) {
            	   op += 3;
               }
           } // if
           else if (!dir.equals("null")) {
        	   op = 13;
        	   
        	   if (!tipo.equals("null")) {
        		   op++;
        	   }
           } // else
           else {
        	   op = 15;
           }
           
           props = PDAO.superBusqueda(op, ini, fin, dir, tipo);
		}
		
	    
		
		if (props.size() == 0) {
			int error = 6;
			session.setAttribute("error", error);
	        
	        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp");
	   		dispatcher.forward(req,res);
		}
		else {
			session.setAttribute("props", props);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Resultados.jsp");
	   		dispatcher.forward(req, res);
		}
	} // void

}
